import json
from datetime import date

from flask import current_app
from flask.json import jsonify
from app import db
from app.duty_table import duty_table
from app.duty_table.models import DutyTable


@duty_table.route('/index', methods=['GET'])
def index():
    item_list = []
    q_list = DutyTable.query.all()
    for i in q_list:
        item_list.append(i.to_dict())

    return jsonify({'code': 0, 'data': item_list, 'msg': 'ok'})


@duty_table.route('/today_event', methods=['GET'])
def today_event():
    """
    今日日程事件
    :return:
    """
    today = str(date.today())
    try:
        queryset = db.session.query(DutyTable)
        query_obj = queryset.filter_by(date_=today).first()
        data = query_obj.to_dict() if query_obj else []
        return jsonify({'code': 0, 'data': data, 'msg': 'OK'})
    except Exception as e:
        current_app.logger.exception(e)
        return jsonify({'code': 5000, 'data': [], 'msg': '系统错误!'})


@duty_table.route('/today_zhibo')
def today_zhibo():
    today = str(date.today())
    try:
        queryset = db.session.query(DutyTable)
        query_obj = queryset.filter_by(date_=today).first()
        data = query_obj.to_dict() if query_obj else []

    except Exception as e:
        pass

@duty_table.route('/monthly_zhibo', methods=['GET', 'POST'])
def monthly_zhibo():
    pass